package com.sms.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.sms.interceptor.AuthInterceptor;
import com.sms.interceptor.TokenInterceptor;


@Configuration
public class WebMvcConfigure implements WebMvcConfigurer {

	@Override
    public void addInterceptors(InterceptorRegistry registry){
        //注册token拦截器
		List<String> excludeUrls = new ArrayList<String>();
		excludeUrls.add("/error");//错误请求
		excludeUrls.add("/login");//登陆
		excludeUrls.add("/logout");//退出
		excludeUrls.add("/randCodeImage");//图形验证码
	    registry.addInterceptor(new TokenInterceptor()).addPathPatterns("/**").excludePathPatterns(excludeUrls);
	    
	    //注册权限拦截器
	    registry.addInterceptor(new AuthInterceptor()).addPathPatterns("/admin/**");
    }
	
	/**
	 * 配置跨域请求
	 */
	@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                //user-token这个自定义header属性不能忘记了
                .allowedHeaders("X-Requested-With","Content-Type","Authorization","user-token")
                .maxAge(3600)
                .allowCredentials(true);
    }
}
